package com.bige.mall.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.bige.common.exception.ServiceException;
import com.bige.common.utils.DateUtils;
import com.bige.common.utils.StringUtils;
import com.bige.mall.domain.*;
import com.bige.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bige.mall.mapper.MallProductMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品Service业务层处理
 * 
 * @author bige
 * @date 2022-01-23
 */
@Service
public class MallProductServiceImpl implements IMallProductService 
{
    @Autowired
    private MallProductMapper mallProductMapper;

    @Autowired
    private IMallProductImgsService mallProductImgsService;

    @Autowired
    private IMallProductStandardService mallProductStandardService;

    @Autowired
    private IMallGoodsService mallGoodsService;

    @Autowired
    private IMallProductDetailsService mallProductDetailsService;

    /**
     * 查询商品
     * 
     * @param productId 商品主键
     * @return 商品
     */
    @Override
    public MallProduct selectMallProductByProductId(Long productId)
    {
        MallProduct mallProduct = mallProductMapper.selectMallProductByProductId(productId);
        List<MallProductImgs> mallProductImgs = mallProductImgsService.selectMallProductImgsListByproductId(productId);
        mallProduct.setMallProductImgs(mallProductImgs);
        List<MallProductStandard> mallProductStandards = mallProductStandardService.selectMallProductStandardListByProductId(productId);
        mallProduct.setMallProductStandards(mallProductStandards);
        List<MallGoods> mallGoods = mallGoodsService.selectMallGoodsListByProductId(productId);
        mallProduct.setMallGoods(mallGoods);
        MallProductDetails mallProductDetails = mallProductDetailsService.selectMallProductDetailsByProductId(productId);
        mallProduct.setDetails(mallProductDetails.getProductDetails());
        return mallProduct;
    }

    /**
     * 查询商品列表
     * 
     * @param mallProduct 商品
     * @return 商品
     */
    @Override
    public List<MallProduct> selectMallProductList(MallProduct mallProduct)
    {
        return mallProductMapper.selectMallProductList(mallProduct);
    }

    /**
     * 新增商品
     * 
     * @param mallProduct 商品
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertMallProduct(MallProduct mallProduct) throws ServiceException {
        mallProduct.setCreateTime(DateUtils.getNowDate());

        List<MallProductImgs> mallProductImgs = mallProduct.getMallProductImgs();
        String productImg = mallProductImgs.get(0).getProductImg();
        if (StringUtils.isEmpty(productImg)){
            throw new ServiceException("请上传首图");
        }
        mallProduct.setFirstImg(productImg);
        List<MallProductStandard> mallProductStandards = mallProduct.getMallProductStandards();
        List<MallGoods> mallGoods = mallProduct.getMallGoods();
        String productDetails = mallProduct.getDetails();
        mallProduct.setDetails("");
        int i = mallProductMapper.insertMallProduct(mallProduct);

        List<MallProductImgs> mallProductImgsList = new ArrayList<MallProductImgs>();
        for (MallProductImgs mallProductImg: mallProductImgs) {
            if (!StringUtils.isEmpty(mallProductImg.getProductImg())){
                mallProductImg.setProductId(mallProduct.getProductId());
                mallProductImgsList.add(mallProductImg);
                //mallProductImgsService.insertMallProductImgs(mallProductImg);
            }
        }
        System.out.println(mallProductImgsList);
        mallProductImgsService.insertMallProductImgsList(mallProductImgsList);

        for (MallProductStandard mallProductStandard: mallProductStandards) {
            mallProductStandard.setProductId(mallProduct.getProductId());
            //mallProductStandardService.insertMallProductStandard(mallProductStandard);
        }
        mallProductStandardService.insertMallProductStandardList(mallProductStandards);

        for (MallGoods mallGood: mallGoods){
            mallGood.setProductId(mallProduct.getProductId());
            //mallGoodsService.insertMallGoods(mallGood);
        }
        mallGoodsService.insertMallGoodsList(mallGoods);

        MallProductDetails mallProductDetails = new MallProductDetails();
        mallProductDetails.setProductId(mallProduct.getProductId());
        mallProductDetails.setProductDetails(productDetails);
        mallProductDetailsService.insertMallProductDetails(mallProductDetails);
        System.out.println(mallProduct.getProductId());

        return i;
    }

    /**
     * 修改商品
     * 
     * @param mallProduct 商品
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateMallProduct(MallProduct mallProduct)
    {
        List<MallProductImgs> mallProductImgs = mallProduct.getMallProductImgs();
        String productImg = mallProductImgs.get(0).getProductImg();
        if (StringUtils.isEmpty(productImg)){
            throw new ServiceException("请上传首图");
        }
        mallProduct.setFirstImg(productImg);
        mallProductImgsService.deleteMallProductImgsByProductId(mallProduct.getProductId());
        List<MallProductImgs> mallProductImgsList = new ArrayList<MallProductImgs>();
        for (MallProductImgs mallProductImg : mallProductImgs){
            if (!StringUtils.isEmpty(mallProductImg.getProductImg())){
                mallProductImg.setProductId(mallProduct.getProductId());
                mallProductImgsList.add(mallProductImg);
                //mallProductImgsService.insertMallProductImgs(mallProductImg);
            }
        }
        mallProductImgsService.insertMallProductImgsList(mallProductImgsList);

        mallProductStandardService.deleteMallProductStandardByProductId(mallProduct.getProductId());
        List<MallProductStandard> mallProductStandards = mallProduct.getMallProductStandards();
        for(MallProductStandard mallProductStandard: mallProductStandards){
            mallProductStandard.setProductId(mallProduct.getProductId());
            //mallProductStandardService.insertMallProductStandard(mallProductStandard);
        }
        mallProductStandardService.insertMallProductStandardList(mallProductStandards);

        mallGoodsService.deleteMallGoodsByProductId(mallProduct.getProductId());
        List<MallGoods> mallGoodsList = mallProduct.getMallGoods();
        for(MallGoods mallGood: mallGoodsList){
            mallGood.setProductId(mallProduct.getProductId());
            //mallGoodsService.insertMallGoods(mallGood);
        }
        mallGoodsService.insertMallGoodsList(mallGoodsList);


        MallProductDetails mallProductDetails = new MallProductDetails();
        mallProductDetails.setProductId(mallProduct.getProductId());
        mallProductDetails.setProductDetails(mallProduct.getDetails());
        mallProductDetailsService.updateMallProductDetails(mallProductDetails);
        mallProduct.setUpdateTime(DateUtils.getNowDate());
        return mallProductMapper.updateMallProduct(mallProduct);
    }

    /**
     * 批量删除商品
     * 
     * @param productIds 需要删除的商品主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteMallProductByProductIds(Long[] productIds)
    {
        mallProductImgsService.deleteMallProductImgsByProductIds(productIds);
        mallProductStandardService.deleteMallProductStandardByProductIds(productIds);
        mallGoodsService.deleteMallGoodsByProductIds(productIds);
        mallProductDetailsService.deleteMallProductDetailsByProductIds(productIds);
        return mallProductMapper.deleteMallProductByProductIds(productIds);
        //return 0;
    }

    /**
     * 删除商品信息
     * 
     * @param productId 商品主键
     * @return 结果
     */
    @Override
    public int deleteMallProductByProductId(Long productId)
    {
        return mallProductMapper.deleteMallProductByProductId(productId);
    }
}
